package annTrain;

import aNN.BackpropagationThread;
import aNN.InputTargetPair;
import aNN.NeuralNetwork;
import aNN.NeuronOutputPair;



public class Tester {
	
	
	public void PerformTesting()
	{
		Tester tester = new Tester();
		tester.Test("CNN_Handwritten_Digits.txt", "", "Training\\TestingSet", 29, 29);
	}
	
	
	public void Test(String nnFile, String classificationFile, String tokenFolder, int inputWidth, int inputHeight)
	{
		NeuralNetwork cnn = new NeuralNetwork();
		TrainerHelper helper = new TrainerHelper();
		System.out.println(cnn.Import(System.getProperty("user.dir") + "\\" + nnFile));
		
		TrainingSet trainingSet = new GenericImageTestingSet(cnn.OutputLayer.Neurons.size(), inputWidth, inputHeight, true);
		trainingSet.LoadTrainingSet(System.getProperty("user.dir") + "\\" + tokenFolder, "", "");
		trainingSet.GenerateRandomSequence(0);
		ResultError resultError = new ResultError(2);
		
		TestEpoch(cnn, helper, trainingSet,
				resultError);
	}
	
	
	private void TestEpoch(NeuralNetwork cnn,
			TrainerHelper helper, TrainingSet trainingSet, ResultError resultError) {
		int iteration = 0;
		resultError.NumErrors = 0;
		resultError.Reset();
		
		while (trainingSet.HasNext())
		{
			InputTargetPair inputsAndTargets = trainingSet.Next();
			if (inputsAndTargets == null)
				continue;
			NeuronOutputPair[] temp = cnn.TempCalculate(helper.CreateInputMap(cnn, inputsAndTargets));
			System.out.println("Iteration: " + iteration);
			System.out.print("Error Square: " + resultError.AddResult(inputsAndTargets.Target, helper.GetNeuronOutputPairLastElements(temp, inputsAndTargets.Target.length)) + "    ");
			int target = helper.GetTarget(inputsAndTargets);
			double[] results = helper.GetNeuronOutputPairLastElements(temp, inputsAndTargets.Target.length);
			int result = helper.GetCalculatedResult(results);
			if (result != target)
			{
				resultError.NumErrors++;
			}
			System.out.print("Num errors: " + resultError.NumErrors + "     ");
			System.out.print(helper.GetTarget(inputsAndTargets) + ": ");
			for (int j = 0; j < inputsAndTargets.Target.length; j++)
			{
				System.out.print(results[j] + " ");
			}
			System.out.println();
			iteration++;
		}
	}
	
	
}
